package CreatorUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import LoginUtil.*;
import LoginUtil.User.UserType;

/**
 * Servlet implementation class LoginServlet
 */
public class CreateNewUserServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, java.io.IOException {

try
{	    

	String username = request.getParameter("usernamebox");
	String password = request.getParameter("passwordbox");
	
     User user = new User(username,password);
     user.setFirstName(request.getParameter("firstnamebox"));
     user.setLastName(request.getParameter("lastnamebox"));
     
     user.setUserType(parseUserType(request.getParameter("typebox")));
     
     // insert into database! yayyy
     
     response.sendRedirect("newUserCreated.jsp");

} 
		
		
catch (Throwable theException) 	    
{
     System.out.println(theException); 
}
       }

private UserType parseUserType(String parameter) {
	UserType type;
	if(parameter.equalsIgnoreCase("admin")) type = UserType.Admin;
	else if(parameter.equalsIgnoreCase("doctor")) type = UserType.Doctor;
	else if(parameter.equalsIgnoreCase("nurse")) type = UserType.Nurse;
	else if(parameter.equalsIgnoreCase("patient")) type = UserType.Patient;
	else type = UserType.Invalid;
	return type;
}
	}

